package display;

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import Util.Util;

import quantity.Quantity;

public class InventoryUpdate extends JFrame {

	private JPanel contentPane;
	private JTextField isbnField, qtyField;
	private JTable table;
	private UpdateActionListener al = new UpdateActionListener();
	private JCheckBox chckbxNew;
	private JComboBox qtyTypeList;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					InventoryUpdate frame = new InventoryUpdate();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public InventoryUpdate() {
		setTitle("Inventory Update");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 538, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{40, 0, 57, 0, 0, 0, 0, 0, 0, 0, 40, 0};
		gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.columnWeights = new double[]{1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		
		JLabel lblIsbn = new JLabel("ISBN");
		GridBagConstraints gbc_lblIsbn = new GridBagConstraints();
		gbc_lblIsbn.insets = new Insets(0, 0, 5, 5);
		gbc_lblIsbn.anchor = GridBagConstraints.EAST;
		gbc_lblIsbn.gridx = 1;
		gbc_lblIsbn.gridy = 0;
		contentPane.add(lblIsbn, gbc_lblIsbn);
		
		isbnField = new JTextField();
		isbnField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent arg0) {
				if (arg0.getKeyCode() == KeyEvent.VK_ENTER)
					Util.printStatusString("You pressed enter!");
			}
		});
		GridBagConstraints gbc_isbnField = new GridBagConstraints();
		gbc_isbnField.gridwidth = 7;
		gbc_isbnField.insets = new Insets(0, 0, 5, 5);
		gbc_isbnField.fill = GridBagConstraints.HORIZONTAL;
		gbc_isbnField.gridx = 2;
		gbc_isbnField.gridy = 0;
		contentPane.add(isbnField, gbc_isbnField);
		isbnField.setColumns(10);
		
		JLabel lblQuantity = new JLabel("Quantity");
		GridBagConstraints gbc_lblQuantity = new GridBagConstraints();
		gbc_lblQuantity.insets = new Insets(0, 0, 5, 5);
		gbc_lblQuantity.anchor = GridBagConstraints.EAST;
		gbc_lblQuantity.gridx = 1;
		gbc_lblQuantity.gridy = 1;
		contentPane.add(lblQuantity, gbc_lblQuantity);
		
		qtyField = new JTextField();
		GridBagConstraints gbc_textField_1 = new GridBagConstraints();
		gbc_textField_1.insets = new Insets(0, 0, 5, 5);
		gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField_1.gridx = 2;
		gbc_textField_1.gridy = 1;
		contentPane.add(qtyField, gbc_textField_1);
		qtyField.setColumns(10);
		
		chckbxNew = new JCheckBox("New");
		GridBagConstraints gbc_chckbxNew = new GridBagConstraints();
		gbc_chckbxNew.insets = new Insets(0, 0, 5, 5);
		gbc_chckbxNew.gridx = 4;
		gbc_chckbxNew.gridy = 1;
		contentPane.add(chckbxNew, gbc_chckbxNew);
		
		JLabel lblType = new JLabel("Type");
		GridBagConstraints gbc_lblType = new GridBagConstraints();
		gbc_lblType.insets = new Insets(0, 0, 5, 5);
		gbc_lblType.anchor = GridBagConstraints.EAST;
		gbc_lblType.gridx = 5;
		gbc_lblType.gridy = 1;
		contentPane.add(lblType, gbc_lblType);
		
		qtyTypeList = new JComboBox();
		qtyTypeList.addItem(Quantity.needed);
		qtyTypeList.addItem(Quantity.onHand);
		qtyTypeList.addItem(Quantity.onOrder);
		qtyTypeList.addItem(Quantity.received);
		qtyTypeList.addItem(Quantity.bba);
		
		GridBagConstraints gbc_comboBox = new GridBagConstraints();
		gbc_comboBox.gridwidth = 4;
		gbc_comboBox.insets = new Insets(0, 0, 5, 5);
		gbc_comboBox.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboBox.gridx = 6;
		gbc_comboBox.gridy = 1;
		contentPane.add(qtyTypeList, gbc_comboBox);
		
		JButton btnAddQuantity = new JButton("Add Quantity");
		btnAddQuantity.addActionListener(al);
		GridBagConstraints gbc_btnAddQuantity = new GridBagConstraints();
		gbc_btnAddQuantity.gridwidth = 3;
		gbc_btnAddQuantity.insets = new Insets(0, 0, 5, 5);
		gbc_btnAddQuantity.gridx = 1;
		gbc_btnAddQuantity.gridy = 2;
		contentPane.add(btnAddQuantity, gbc_btnAddQuantity);
		
		JButton btnResetForm = new JButton("Reset Form");
		btnResetForm.addActionListener(al);
		GridBagConstraints gbc_btnResetForm = new GridBagConstraints();
		gbc_btnResetForm.gridwidth = 3;
		gbc_btnResetForm.insets = new Insets(0, 0, 5, 5);
		gbc_btnResetForm.gridx = 4;
		gbc_btnResetForm.gridy = 2;
		contentPane.add(btnResetForm, gbc_btnResetForm);
		
		JButton btnImport = new JButton("Import");
		btnImport.addActionListener(al);
		GridBagConstraints gbc_btnImport = new GridBagConstraints();
		gbc_btnImport.gridwidth = 2;
		gbc_btnImport.insets = new Insets(0, 0, 5, 5);
		gbc_btnImport.gridx = 7;
		gbc_btnImport.gridy = 2;
		contentPane.add(btnImport, gbc_btnImport);
		
		JLabel ed = new JLabel("");
		GridBagConstraints gbc_ed = new GridBagConstraints();
		gbc_ed.insets = new Insets(0, 0, 5, 5);
		gbc_ed.gridx = 9;
		gbc_ed.gridy = 3;
		contentPane.add(ed, gbc_ed);
		
		JLabel author = new JLabel("");
		GridBagConstraints gbc_author = new GridBagConstraints();
		gbc_author.insets = new Insets(0, 0, 5, 5);
		gbc_author.gridx = 1;
		gbc_author.gridy = 4;
		contentPane.add(author, gbc_author);
		
		JLabel title = new JLabel("");
		GridBagConstraints gbc_title = new GridBagConstraints();
		gbc_title.insets = new Insets(0, 0, 5, 5);
		gbc_title.gridx = 3;
		gbc_title.gridy = 4;
		contentPane.add(title, gbc_title);
		
		table = new JTable();
		table.setFillsViewportHeight(true);
		
		DefaultTableModel tableM = (DefaultTableModel)table.getModel();
		
		tableM.setColumnIdentifiers(new String[]{"Qty","Type"});
		
		JScrollPane scrollPane = new JScrollPane(table);
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridwidth = 11;
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 0;
		gbc_scrollPane.gridy = 5;
		contentPane.add(scrollPane, gbc_scrollPane);
	}
	
	private class UpdateActionListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			
			JButton b = (JButton)e.getSource();
			
			if (b.getText().equals("Add Quantity")) {
				ToDoDialog todo = new ToDoDialog();
				todo.setVisible(true);
			}
			else if (b.getText().equals("Reset Form")) {
				isbnField.setText("");
				qtyField.setText("");
				chckbxNew.setSelected(false);
				qtyTypeList.setSelectedIndex(0);
			}
			else if (b.getText().equals("Import")) {
				
			}
			// TODO Auto-generated method stub
			
		}
		
	}

}
